/**
 * JAVACC DEMO 1.0
 */
package com.apache.client;

import com.apache.api.vo.ResultEntity;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * description:  用户中心客户端协议调用工具
 *
 */
public class UctProtoclClient {

    private static UctProtoclClient instance;

    private Map<String, String> actionMap = new HashMap<String, String>(1);

    private UctProtoclClient() {
        actionMap.put("dict", "/dict/service/doService.action");
        actionMap.put("info", "/info/service/doService.action");
        actionMap.put("audit", "/audit/service/doService.action");
        actionMap.put("atta", "/attachment/service/doService.action");
        actionMap.put("funds", "/funds/service/doService.action");
        actionMap.put("wcm", "/wcm/service/doService.action");
        actionMap.put("sso", "/passport/outside/doService.action");
        actionMap.put("member", "/member/service/doService.action");
        actionMap.put("rpc", "/rpc/service/doService.action");
        actionMap.put("seq", "/seq/service/doService.action");
        actionMap.put("ius", "/ius/service/doService.action");
        actionMap.put("uct", "/uct/service/doService.action");
    }

    public static UctProtoclClient getInstance() {
        if (null == instance) {
            instance = new UctProtoclClient();
        }
        return instance;
    }

    public ResultEntity doService(String beanId, String methodKey, Map<String, Object> params) {
        InterfaceRegister register = getInterfaceRegister("uct", "uctServer_ip", "uctServer_port");
        return LoadRpcService.service().doServiceClient(beanId, methodKey, params, register);
    }

    public ResultEntity doDictService(String beanId, String methodKey, Map<String, Object> params) {
        return LoadRpcService.service().doServiceClient(beanId, methodKey, params,
                getInterfaceRegister("dict", "dictServer_ip", "dictServer_port"));
    }

    public ResultEntity doInfoService(String beanId, String methodKey, Map<String, Object> params) {
        return LoadRpcService.service().doServiceClient(beanId, methodKey, params,
                getInterfaceRegister("info", "info_release_url", "info_release_port"));
    }

    public ResultEntity doAuditService(String beanId, String methodKey,
            Map<String, Object> params) {
        return LoadRpcService.service().doServiceClient(beanId, methodKey, params,
                getInterfaceRegister("audit", "audit_server_url", "audit_server_port"));
    }

    public InterfaceRegister getInterfaceRegister(String actionKey, String urlKey, String portKey) {
        InterfaceRegister register = new InterfaceRegister();
        String reqUrl = ToolsUtil.getInstance().getValueByKey(urlKey);
        if (StrUtil.isNotNull(reqUrl)) {
            String infoUrl = reqUrl + StrUtil.doNull(actionMap.get(actionKey),
                    "/" + actionKey + "/service/doService.action");
            if (reqUrl.startsWith("https:")) {
                register.setCallType("https");
                register.setAddress(infoUrl);
            } else if (reqUrl.startsWith("http:")) {
                register.setCallType("http");
                register.setAddress(infoUrl);
            } else {
                String port = ToolsUtil.getInstance().getValueByKey(portKey);
                register.setAddress(reqUrl);
                register.setPort(port);
                register.setCallType("socket");
            }
        } else {
            register.setCallType("socket");
            register.setAddress("127.0.0.1");
        }
        return register;
    }

    public ResultEntity getSeqNextValue(String seqEname,int strLen,int mode){
        InterfaceRegister register = UctProtoclClient.getInstance().getInterfaceRegister("seq", "info_release_url", "info_release_port");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("seqEname", seqEname);
        map.put("strLen",String.valueOf(strLen));
        map.put("seqMode", String.valueOf(mode));
        ResultEntity resultEntity = LoadRpcService.service().doServiceClient("seqService", "seqNextValue", map, register);
        return resultEntity;
    }
}
